import bobingService from '@/api/bobing-api.js'
import { getUser, setUser } from '@/utils/auth'
import {
    Dialog,
    Button,
    NoticeBar,
    Popup,
    Cell,
    CellGroup,
    Field,
    Toast,
    NavBar,
    Image,
    Row,
    Tag,
    PullRefresh,
    List,
    Sticky 
} from 'vant'

export default {
    name: 'My',
    components: {
        [Dialog.name]: Dialog,
        [Button.name]: Button,
        [NoticeBar.name]: NoticeBar,
        [Popup.name]: Popup,
        [Cell.name]: Cell,
        [CellGroup.name]: CellGroup,
        [Toast.name]: Toast,
        [Field.name]: Field,
        [NavBar.name]: NavBar,
        [Image.name]: Image,
        [Tag.name]: Tag,
        [Row.name]: Row,
        [PullRefresh.name]:PullRefresh,
        [List.name]:List,
        [Sticky .name]:Sticky 
    },
    data() {
        return {
            isLoading: false,
            finished:false,
            loading: false,
            prizes: [],
            message:''
        }
    },
    methods: {
        //返回
        onClickLeft() {
            this.$router.push(
                {
                    path: '/Home'
                })
        },
        loadPrizes() {
            let _this = this
            let user = getUser()
            _this.prizes = []
            if (user) {
                let currentUser = JSON.parse(user)
                if(currentUser.authorizationCode){
                    _this.isLoading = true
                    _this.loading = true
                    bobingService.getPrizes(currentUser.authorizationCode).then(response => {
                        let totalIntegral = 0
                        response.forEach(item => {
                            _this.prizes.push(item)
                            totalIntegral += item.integral
                        })
                        _this.message = '亲爱的' + currentUser.userName + '您目前的总积分是：' + totalIntegral
                        _this.isLoading = false
                        _this.loading = false
                        _this.finished = true
                        Toast('加载完成')
                    }, error => {
                        _this.isLoading = false
                        _this.loading = false
                        _this.finished = true
                        Toast('加载失败')
                    })
                }else{
                    _this.isLoading = false
                    _this.loading = false
                    Toast('请先注册')
                }
            }else{
                _this.isLoading = false
                _this.loading = false
                _this.message = '亲爱的贵宾您目前的总积分是：0'
            }
        },
        onLoad() {
            this.loadPrizes()
        },
        onRefresh(){
            this.loadPrizes()
        },
    },
    mounted() {
        //this.loadPrizes()
    },
    watch: {
        '$route.path': function (newVal, oldVal) {
            if (oldVal === '/Home') {
                this.loadPrizes()   
            }
        }
    },
}